﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6策略模式
{
    /// <summary>
    /// 现金收费抽象类
    /// </summary>
    abstract class CashSuper
    {
        /// <summary>
        /// 现金收费超类抽象方法
        /// </summary>
        /// <param name="money">原价</param>
        /// <returns>当前价</returns>
        public abstract double AcceptCash(double money);
    }
    /// <summary>
    /// 正常收费子类
    /// </summary>
    class CashNormal : CashSuper
    {
        /// <summary>
        /// 重写收费方法
        /// </summary>
        /// <param name="money">原价</param>
        /// <returns>当前价</returns>
        public override double AcceptCash(double money)
        {
            return money;
        }
    }
    /// <summary>
    /// 打折收费子类
    /// </summary>
    class CashRebate : CashSuper
    {
        /// <summary>
        /// 打折数
        /// </summary>
        private double moneyRebate = 1d;
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="moneyRebate"></param>
        public CashRebate(string moneyRebate)
        {
            this.moneyRebate = double.Parse(moneyRebate);
        }
        /// <summary>
        /// 重写收费方法
        /// </summary>
        /// <param name="money">原价</param>
        /// <returns>当前价</returns>
        public override double AcceptCash(double money)
        {
            return money * moneyRebate;
        }
    }
    /// <summary>
    /// 返利收费子类
    /// </summary>
    class CashReturn : CashSuper
    {
        private double moneyCondition = 0.0d;
        private double moneyReturn = 0.0d;
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="moneyRebate"></param>
        public CashReturn(string moneyCondition, string moneyReturn)
        {
            this.moneyCondition = double.Parse(moneyCondition);
            this.moneyReturn = double.Parse(moneyReturn);
        }
        /// <summary>
        /// 重写收费方法
        /// </summary>
        /// <param name="money">原价</param>
        /// <returns>当前价</returns>
        public override double AcceptCash(double money)
        {
            double result = money;
            if (money >= moneyCondition)
            {
                result = money - Math.Floor(money / moneyCondition) * moneyReturn;
            }
            return result;
        }
    }
    /// <summary>
    /// 配置类 
    /// </summary>
    class CashContext
    {
        private CashSuper cs = null;
        /// <summary>
        /// 收费策略对象
        /// </summary>
        /// <param name="type"></param>
        public CashContext(string type)
        {
            switch (type)
            {
                case "正常收费":
                    CashSuper cs0 =new CashNormal();
                    cs = cs0;
                    break;
                case "满300返100":
                    CashSuper cr1 =new CashReturn("300","100");
                    cs = cr1;
                    break;
                case "打八折":
                    CashSuper cr2 =new CashRebate("0.8");
                    cs = cr2;
                    break;
            }
        }
        /// <summary>
        /// 获取计算后成果
        /// </summary>
        /// <param name="money">原价</param>
        /// <returns>当前价</returns>
        public double GeResult(double money)
        {
            return cs.AcceptCash(money);
        }
    }
}
